<?php
/**
 * Created by PhpStorm.
 * User: Simpson
 * Date: 2016/10/9
 * Time: 19:02
 */

namespace App\Repository;


use App\Model\Lottery;
use App\Model\Prize;
use Log;
class LotteryRepository
{

    public function prizes()
    {
        return Prize::all();
    }


    /**
     * 传入随机数，获取奖品对象
     * @param $random
     * @return mixed
     */
    public function getPrize($random)
    {
        $prize = Prize::where('left_interval', '<=', $random)->where('right_interval', '>', $random)->first();

        if($prize->amount == 0){
            $prize = Prize::where('name', '谢谢参与')->select(['id'])->first();
            return $prize;
        }

        $prize->amount -= 1;
        $prize->save();

        return $prize;
    }

    /**
     * 返回奖品列表
     *
     * @return mixed
     */
    public function prizeList()
    {
        $prize_list = Prize::get(['id', 'name']);

        return $prize_list;
    }

    /**
     * 记录获奖者和奖品
     * 
     * @param $openId
     * @param $prizeId
     * @return bool
     */
    public function addLottery($openId, $prizeId)
    {

        $lottery = Lottery::create([
            'open_id' => $openId,
            'prize_id' => $prizeId,
        ]);

        return $lottery ? true : false;
    }

    public function checkLottery($openId)
    {

        $lottery = Lottery::where('open_id', $openId)->first();

        return $lottery ? true : false;

    }

    public function lotteryList()
    {
        $lottery_list = Lottery::orderBy('id', 'desc')->get();

        return $lottery_list;
    }
}